package com.roadjava.favorite.service.impl;


import com.roadjava.favorite.bean.entity.FavoriteDO;
import com.roadjava.favorite.bean.entity.FavoriteEQ;
import com.roadjava.favorite.config.RedisCache;
import com.roadjava.favorite.mapper.FavoriteMapper;
import com.roadjava.favorite.service.FavoriteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

@Service
public class FavoriteServiceImpl implements FavoriteService {

    @Autowired
    private FavoriteMapper favoriteMapper;

    @Autowired
    private RedisCache redisCache;

    @Override
    public void addFavorite(FavoriteEQ favorite) {
        // 将收藏信息存入Redis
        String key = "favorite:" + favorite.getResourceId() + ":" + favorite.getId();
        String unfavoritedKey = "unfavorite:" + favorite.getResourceId() + ":" + favorite.getId();
        if (redisCache.hasKey(unfavoritedKey)) {
            redisCache.deleteObject(unfavoritedKey);
            redisCache.setCacheObject(key, favorite);
        }else {
            redisCache.setCacheObject(key, favorite);
        }
    }

    @Override
    public void removeFavoriteByResourceAndUser(Long resourceId, Long userId) {
        // 检查Redis中是否存在相同的收藏记录
        String key = "favorite:" + resourceId + ":" + userId;
        String unfavoritedKey = "unfavorite:" + resourceId + ":" + userId;
        if (redisCache.hasKey(key)) {
            redisCache.deleteObject(key);
        }else {
            redisCache.setCacheObject(unfavoritedKey, true);
        }

    }

    @Override
    public List<FavoriteDO> getUserFavorites(Long userId) {
        return favoriteMapper.getUserFavorites(userId);
    }

    @Override
    public Long getFavoriteCount(Long resourceId) {
        return favoriteMapper.getFavoriteCount(resourceId);
    }

    @Override
    public void removeFavoritesByResource(Long resourceId) {
        favoriteMapper.removeFavoritesByResource(resourceId);
    }

    @Override
    public boolean isFavoritedByUser(Long resourceId, Long userId) {
        return favoriteMapper.isFavoritedByUser(resourceId, userId) > 0;
    }

    @Scheduled(fixedRate = 60000) // 每分钟执行一次
    public void syncFavoritesToDatabase() {
        Set<String> favoriteKeys = (Set<String>) redisCache.keys("favorite:*");
        Set<String> unfavoritedKeys = (Set<String>) redisCache.keys("unfavorite:*");

        for (String key : favoriteKeys) {
            FavoriteEQ favoriteEQ = (FavoriteEQ) redisCache.getCacheObject(key);
            if (favoriteEQ != null) {
                // 插入或更新数据库
                favoriteMapper.addFavorite(favoriteEQ);

                // 删除Redis中的记录
                redisCache.deleteObject(key);
            }
        }

        for (String key : unfavoritedKeys) {
            String[] parts = key.split(":");
            Long resourceId = Long.parseLong(parts[1]);
            Long userId = Long.parseLong(parts[2]);

            // 删除数据库中的记录
            favoriteMapper.removeFavoriteByResourceAndUser(resourceId, userId);

            // 删除Redis中的记录
            redisCache.deleteObject(key);
        }
    }
}